/******************************************************************************
 Copyright (c) 2024 Qualcomm Technologies International, Ltd.
 All Rights Reserved.
 Qualcomm Technologies International, Ltd. Confidential and Proprietary.

******************************************************************************/

#include "csr_synergy.h"

#ifdef CSR_STREAMS_ENABLE
#include "gatt_ots_client_streams.h"
#include "gatt_ots_client_common_util.h"
#include "gatt_ots_client_read_object.h"

static void otscProcessStreamsData(GattOtsClient *inst, MessageMoreData *mmd)
{
    Source source             = mmd->source;
    CsrBtConnId btConnId      = CM_CREATE_L2CA_CONN_ID(SinkGetL2capCid(StreamSinkFromSource(source)));
    GOTSC *gattOtsClient      = NULL;

    if (OtscFindSrvcHndlByConnId(&inst->serviceHandleList, btConnId, &gattOtsClient))
    {
        GATT_OTS_CLIENT_INFO("otscProcessStreamsData, source:%p sourceSize %u", source, SourceBoundary(source));
        OtscPrepareAndSendObjectContent(gattOtsClient, (uint8 *)source, SourceBoundary(source));
    }
    else
    {
        /* Did not find right service handle; drop the complete data */
        SourceDrop(source, SourceSize(source));
    }

    /* Set this to NULL to prevent getting freed now */
    mmd = NULL;
}

void OtscMessageMoreDataHandler(GattOtsClient *inst, void *msg)
{
    otscProcessStreamsData(inst, (MessageMoreData *)msg);
}

void OtscMessageMoreSpaceHandler(GattOtsClient *inst, void *msg)
{
    /* TODO : Add handling for MMS */
    CSR_UNUSED(inst);
    CSR_UNUSED(msg);
}

void OtscStreamsRegister(CsrBtCmL2caTpConnectCfm *cfm)
{
    CsrUint16 cid = CM_GET_UINT16ID_FROM_BTCONN_ID(cfm->btConnId);

    if (CsrStreamsRegister(cid, L2CAP_ID, GATT_OTS_CLIENT_IFACEQUEUE))
    {
        CsrStreamsSourceHandoverPolicyConfigure(cid,
                                                L2CAP_ID,
                                                SOURCE_HANDOVER_ALLOW_WITHOUT_DATA);
    }
}
#endif /* CSR_STREAMS_ENABLE */

